<?php

class ProjectsWindow
{
  	public function __construct()
    {   
        $this->prwindow = &new GtkWindow;
        //$this->prwindow->connect('delete-event', array(&$this, 'Hide'));
        //$this->prwindow->connect_simple('key_press_event', array(&$this,'KeyTest'), $this->prwindow);
        $this->prwindow->set_title(_a('Projects'));
        $this->prwindow->set_position(GTK_WIN_POS_CENTER);
        $this->prwindow->set_default_size(668,546);
        $this->prwindow->realize();
        $hbox = new GtkHBox;
        $this->prwindow->add($hbox);
        $pr = new Projects();
        $pr->ReadProjects();
        $projects = $pr->Projects;

        $desc[0] = _a('Project Path');
        $desc[1] = _a('Remote Editing Server');
        $desc[2] = _a('FTP Server IP');
        $desc[3] = _a('FTP Server User');
        $desc[4] = _a('FTP Server Password');
        $desc[5] = _a('FTP Server Path');
        $desc[6] = _a('CVS Server');
        $desc[7] = _a('CVS User');
        $desc[8] = _a('Database IP');
        $desc[9] = _a('Database User');
        $desc[10] = _a('Database Password');
        $desc[11] = _a('Database Name');
        $desc[12] = _a('Database Type');
        $desc[13] = _a('Icon') . ' .xpm (16x16)';
        
        $tooltip[1] = _a('Apache Server where PhpGtkEditor Service is running, example ^1 or ^2', '"192.168.0.62"', '"192.168.0.62/dir"');
        $tooltip[5] = _a('Path in the FTP Server that will be used to send this project files');

        foreach ($projects as $key => $content)
        {
            $i = 0;
            foreach ($content as $var => $value)
            {
                $pass = false;
                $kind = null;
                $edit = true;
                if ($var == 'db_pass' or $var == 'ftp_pass' )
                {
                    $pass = true;
                    $value = base64_decode($value);
                }
                if ($var == 'db_type')
                {
                    $kind = array('pgsql', 'mysql');
                    $edit = false;
                }
                if ($var == 'directory')
                {
                    $kind = 'files2';
                }
                if ($var == 'icon')
                {
                    $kind = 'files';
                }
                $Vars[$key][] = array($value, $desc[$i], $pass, $kind, $edit, $tooltip[$i]);
                $atts[$var] = true;
                $i ++;
            }
        }

        foreach ($desc as $key => $desc)
        //foreach ($atts as $var => $att)
        {
            $pass = false;
            $kind = null;
            $edit = true;
            if (($key == 4) or ($key == 10))
            {
                $pass = true;
                $value = base64_decode($value);
            }
            if ($key == 12)
            {
                $kind = array('pgsql', 'mysql');
                $edit = false;
            }
            if ($key == 0)
            {
                $kind = 'files2';
            }
            if ($key == 13)
            {
                $kind = 'files';
            }
            if ($key == 1)
            {
                $value = 'http://';
            }
            $Plus[] = array('', $desc, $pass, $kind, $edit);
        }

        $slot_ok = array(&$this, 'onProjects');
        $slot_del= array(&$this, 'DeleteProject');
        $this->Colors = &new Preferences(&$hbox);
        $this->Colors->SetTitleImage(images . 'project_bar.xpm');
        $this->Colors->SetTitle(_a('Projects'));
        $this->Colors->SetPlus($Plus);
        $this->Colors->SetSaveButton(true);
        $this->Colors->SetOk($slot_ok, _a('Save'));
        $this->Colors->SetDel($slot_del);
        $this->Colors->SetStatus(_a('Loaded from Projects directory'));
        $this->Colors->SetPixmap(images . 'projects.xpm');
        $this->Colors->SetListTitle(_a('Project Name'));
        $this->Colors->BuildForms($Vars);

        $this->prwindow->show_all();
    }
    
    /**
	 * Method to erase a project file when the user clicks the button to do so
	 */
    function DeleteProject()
    {
        $project = $this->Colors->Current();
        @unlink(APP_PROJECT ."/$project");
    }
      
    function onProjects()
    {
        $return = $this->Colors->GetVars();
        $project = key($return);
        $schema = $return[$project];

        $projects['directory']   = $schema[0];
        $projects['remote']      = $schema[1];
        $projects['ftp_ip']      = $schema[2];
        $projects['ftp_user']    = $schema[3];
        $projects['ftp_pass']    = $schema[4];
        $projects['ftp_dir']     = $schema[5];
        $projects['server']      = $schema[6];
        $projects['cvs_user']    = $schema[7];
        $projects['db_ip']       = $schema[8];
        $projects['db_user']     = $schema[9];
        $projects['db_pass']     = $schema[10];
        $projects['db_name']     = $schema[11];
        $projects['db_type']     = $schema[12];
        $projects['icon']        = $schema[13];

        $fd = fopen (APP_PROJECT ."/{$project}", "w");
        if ($fd)
        {
            fwrite ($fd, "<?\n");
            fwrite ($fd, "\$directory               =  '" .   $schema[0] . "';\n");
            fwrite ($fd, "\$remote                  =  '" .   $schema[1] . "';\n");
            fwrite ($fd, "\$ftp_ip                  =  '" .   $schema[2] . "';\n");
            fwrite ($fd, "\$ftp_user                =  '" .   $schema[3] . "';\n");
            fwrite ($fd, "\$ftp_pass                =  '" .   base64_encode($schema[4]) . "';\n");
            fwrite ($fd, "\$ftp_dir                 =  '" .   $schema[5] . "';\n");
            fwrite ($fd, "\$server                  =  '" .   $schema[6] . "';\n");
            fwrite ($fd, "\$cvs_user                =  '" .   $schema[7] . "';\n");
            fwrite ($fd, "\$db_ip                   =  '" .   $schema[8] . "';\n");
            fwrite ($fd, "\$db_user                 =  '" .   $schema[9] . "';\n");
            fwrite ($fd, "\$db_pass                 =  '" .   base64_encode($schema[10]) . "';\n");
            fwrite ($fd, "\$db_name                 =  '" .   $schema[11] . "';\n");
            fwrite ($fd, "\$db_type                 =  '" .   $schema[12]. "';\n");
            fwrite ($fd, "\$icon                    =  '" .   $schema[13]. "';\n");
            fwrite ($fd, "?>");
        }
        fclose($fd);
        $this->Projects[$project] = $projects;
        $this->prwindow->hide();
        $app = Application::singleton();
        $app->TextEditor->ReloadProjectTree();
        
    }
}    